﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HsTec.Mobile.Green.SqlServerDAL;
using Baimasoft.Web.UI;

public partial class Study_StudyForm : Baimasoft.Web.UI.PageFormTop
{
    Study MyStudy = new Study();

    protected void TypeDataBind()
    {
        ddlType.DataSource = MyStudy.GetTypeInfos("");
        ddlType.DataBind();
        ddlType.Items.Insert(0, "全部类别");
        ddlType.Items[0].Value = "0";
    }


    /// <summary>
    /// 初始化表单信息
    /// </summary>
    protected override void InitForm()
    {
        if (!Page.IsPostBack)
        {
            try
            {
                TypeDataBind();
                try
                {
                    Indicator.Text = Request.QueryString["sIndicator"].ToString();
                }
                catch
                {
                }

                string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
                if (sEditStatus == "2")
                {
                    string sID = this.Request.QueryString["ID"].ToString().Trim();
                    DataTable MyDT = MyStudy .GetInfos (" a.ID = '" + sID + "' ");
                    foreach (DataRow dr in MyDT.Rows)
                    {
                        ddlType.Text = dr["TypeID"].ToString().Trim();
                        this.txtTitle.Text = dr["Title"].ToString().Trim();
                        this.txtkeyWords.Text = dr["KeyWords"].ToString().Trim();

                        this.txtWriter.Text = dr["Writer"].ToString().Trim();
                        this.Content.Text = dr["Content"].ToString().Trim();
                        this.rdoIsShow.Text = dr["IsShow"].ToString().Trim();
                        this.txtAddtime.Text = dr["CreatDt"].ToString().Trim();
                    }
                }
                else
                {
                    this.txtAddtime.Text = DateTime.Now.AddDays(-1).ToString();
                }
            }
            catch
            {
            }
        }
    }


    /// <summary>
    /// 保存数据
    /// </summary>
    protected override void SavaData()
    {
        try
        {
            string sid = "";

            if (this.Request.Params["ID"] != null)
            {
                sid = this.Request.Params["ID"].Trim();
            }
            else
            {
                sid = "-1";
            }

            string sTypeID = ddlType.SelectedValue.ToString().Trim();
            string sTitle = txtTitle.Text.ToString().Trim();
            string sKeyWords = txtkeyWords.Text.ToString().Trim();

            string sWriter = txtWriter.Text.ToString().Trim();
            string sContent = Content.Text.ToString().Trim();
            string sIsShow = rdoIsShow.SelectedValue.ToString().Trim();
            DateTime DAddtime = Convert.ToDateTime(this.txtAddtime.Text.ToString().Trim());//发布日期

            string sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "1")
            {
                int iback = MyStudy.InsertInfo(sTypeID, sTitle, sContent, sKeyWords, sWriter, sIsShow, DAddtime);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');location.href='StudyManage.aspx';</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                int iback = 0;
                try
                {
                    iback = MyStudy .UpdateInfo (sTypeID ,sTitle ,sContent ,sKeyWords ,sWriter ,sIsShow ,DAddtime , sid);
                }
                catch
                {
                }

                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');location.href='StudyManage.aspx';</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            Message.AlertMsg(this, "保存失败");
        }

    }

}
